;; get item from list
(define (item-get ilist i) 
   (define (__item-get ilist i count)
      (if (= i count) (car ilist)
          (__item-get (cdr ilist) i (+ count 1))))
   (__item-get ilist i 0))
   
;; (item-get card-people 2)
              

;; reverse list
(define (reverse ilist) 
  (define size (len ilist))
  (define (__reverse ilist newlist)
      (if (null? (cdr ilist)) (cons (car ilist) newlist)
          (__reverse (cdr ilist) (cons (car ilist) newlist))))
  (cond ((= 0 size) ilist)
        (else (__reverse ilist (list)))))
;;
  
;;item-select
(define (item-select ilist i)   
    (define (__item-select ilist ret newlist i count)    
      (define (dispatch m)
          (cond ((= m 0) ret)
                ((= m 1) (reverse newlist)))) 
    (cond ((null? ilist) dispatch)
          ((= i count) (__item-select (cdr ilist) (cons (car ilist) ret) newlist i (+ count 1)))          
          (else (__item-select (cdr ilist) ret (cons (car ilist) newlist) i (+ count 1)))))
  (__item-select ilist (list) (list) i 0))
;;


;; card list the game data      
(define card-people (list 'yellow 'violet 'green 'purple 'red 'white))
(define card-room (list 'room-1 'room-2 'room-3 'room-4 'room-5 'room-6))
(define card-weapon (list 'sword 'stick 'gun 'rope 'pipe 'wrench))
;;
;; card list the game data   
(define guess-player (list 'yellow 'violet 'green 'red))   
(define guess-people (list 'yellow 'violet 'green 'purple 'red 'white))
(define guess-room (list 'room-1 'room-2 'room-3 'room-4 'room-5 'room-6))
(define guess-weapon (list 'sword 'stick 'gun 'rope 'pipe 'wrench))
;;


;; calc length
(define (len items) 
  (define (__len items value)
  (if (null? items) value
  (__len (cdr items) (+ value 1))))
  (cond ((null? items) 0)
        (else (__len items 0))))
;;


;;get card from one list 
(define (get-card ilist m)
  (define det (random (len ilist)))
  (define selected (item-select ilist det))
  (cond ( (= m 0) (begin (set! card-people (selected 1)) (selected 0) ))
        ( (= m 1) (begin (set! card-room (selected 1)) (selected 0) ))
        ( (= m 2) (begin (set! card-weapon (selected 1)) (selected 0) ))
  ))
;;

;;get card from one list without removing
(define (guess-card ilist)
  (define det (random (len ilist)))
  (define selected (item-select ilist det))
  (selected 0)
  )
;;

;; get random card
(define (get-random-card)
  (define m (random 3))  
  (cond ( (= m 0) (cond ((= 0 (len card-people)) (get-random-card)) (else (get-card card-people m))))
        ( (= m 1) (cond ((= 0 (len card-room))   (get-random-card)) (else (get-card card-room m))))
        ( (= m 2) (cond ((= 0 (len card-weapon)) (get-random-card)) (else (get-card card-weapon m))))
))
;;


;; people class
(define (people name __card1 __card2 __card3)
       (define card1 __card1)
       (define card2 __card2)
       (define card3 __card3)            
       (define (dispatch m)
         (cond ((= m 0) name)
               ((= m 1) (list (car __card1) (car __card2) (car __card3))) ;; I don`t know why it changed from var to list
               )
         )
       dispatch)
(define (name i) (i 0))
(define (card i) (i 1))
;;
  

(define (element-matching det target-list ret)
  (cond ((null? target-list) ret)
        ((equal? det (car target-list)) (element-matching det (cdr target-list) (cons det ret)))
        (else (element-matching det (cdr target-list) ret))))

(define (__list-matching list1 list2 ret)
  (cond ((null? list1) ret)        
        (else (__list-matching (cdr list1) list2 (element-matching (car list1) list2 ret)))))

(define (list-matching list1 list2)
        (reverse (__list-matching list1 list2 (list))))

(define (filter-called called enum)
  (if (equal? called ((car enum) 0)) ((car enum) 1)
              (filter-called called (cdr enum))))
  
(define (call-to-room caller called det)
  (list-matching det (filter-called called players)))


(define answer (people 'answer (get-card card-people 0) (get-card card-room 1) (get-card card-weapon 2)))
(define player1 (people 'red (get-random-card) (get-random-card) (get-random-card) ))
(define player2 (people 'yellow (get-random-card) (get-random-card) (get-random-card) ))
(define player3 (people 'violet (get-random-card) (get-random-card) (get-random-card) ))
(define player4 (people 'green (get-random-card) (get-random-card) (get-random-card) ))
(define players (list player1 player2 player3 player4))

  
  
  
(define (game-help)
  (display "(call-to-room 'caller 'called (list 'suspect 'weapon 'room-number))"))
(call-to-room (name player1) (car (guess-card guess-people)) (list 'yellow 'gun 'room-1))




;;player AI
;;(call-to-room (name player1) (car (guess-card guess-player)) (list (car (guess-card guess-people)) (car (guess-card guess-weapon)) (car (guess-card guess-room))))
;;






